package queue;
/*
* 数组实现环形队列
* */
public class CircleQueue {
    public static void main(String[] args) {
    	MyCircleQueue queue = new MyCircleQueue(5);
    	queue.push(1);
    	queue.push(2);
    	queue.push(3);
    	queue.push(4);


    	queue.toList();
    	System.out.println("=====================================");
    	queue.pop();
    	queue.push(6);
		queue.toList();
    }
}
class MyCircleQueue{
    private int front = 0;
    private int rear = 0;
    private int maxSize;
    private int[] circleQueue;


    public MyCircleQueue(int maxSize){
        this.maxSize = maxSize;
        circleQueue = new int[maxSize];
    }

    //入队列
    public void push(int num){
        //判满
    	if(isFull()) {
    		throw new RuntimeException("队列已满");
    	}else {
    		circleQueue[rear] = num;
			rear = (rear+1)%maxSize;
    	}
    }

    //出队列
    public int pop() {
    	//判空
    	if(isEmp()) {
    		throw new RuntimeException("队列为空");
    	}else {
    		int temp = circleQueue[front];
			front = (front+1)%maxSize;
			return temp;
    	}
    }
    //判断队列是否为满 true为满
    public boolean isFull(){
    	return (rear+1)%maxSize == front;
    }
    //判断队列是否为空
    public boolean isEmp() {
    	return rear == front;
    }

	//返回队列实际数量
	public int numCount(){
		return (rear+maxSize-front)%maxSize;
	}

    //遍历队列
    public void toList() {
    	//判空
    	if(isEmp()) {
			throw new RuntimeException("队列为空");
    	}else {
    		for(int i=front;i<front+numCount();i++) {
    			System.out.println("队列的第"+i+"个数为"+circleQueue[i%maxSize]);
    		}
    	}
    }

}
